/*
	Visp is a visual performance application (VJ app) built on Adobe's AIR runtime framework.
	Copyright (C) 2007 Mike Creighton
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Mike Creighton can be found at http://www.mikecreighton.com	
*/
package com.visp.transitions
{
	import com.visp.transitions.AbstractTransition;
	import com.visp.transitions.TransitionManager;
	
	import flash.display.BitmapData;
	import flash.events.IEventDispatcher;
	import flash.geom.ColorTransform;
	import flash.utils.*;

	public class CrossfadeTransition extends AbstractTransition
	{
		private var _increment : Number;
		private var _activeTrans : ColorTransform;
		private var _queuedTrans : ColorTransform;
		private var _queuedBmp : BitmapData;
		private var _activeBmp : BitmapData;
		
		public function CrossfadeTransition(target:IEventDispatcher=null)
		{
			super(target);
			this._activeBmp = this._output.clone();
			this._queuedBmp = this._output.clone();
		}
		
		override public function start(queued : BitmapData, active : BitmapData) : void 
		{
			super.start(queued, active);
			this._activeTrans = new ColorTransform(1, 1, 1, 1);
			this._queuedTrans = new ColorTransform(1, 1, 1, 0);
		}
		
		override public function render(queued:BitmapData, active:BitmapData) : BitmapData
		{
			// Determine how much alpha to add to each iteration
			// Start by finding out how far into the transition we are.
			this._output.fillRect(this._output.rect, 0x00000000);
						
			var dur : Number = TransitionManager.getInstance().getDuration();
			var halfDur : Number = dur / 2;
			
			// We need to start the alpha halfway through.
			if(getTimer() - this._startTime > dur / 2){
				this._queuedTrans.alphaMultiplier = 1;
			} else {
				this._queuedTrans.alphaMultiplier = (getTimer() - this._startTime) / (halfDur);
			}
			
			if(this._queuedTrans.alphaMultiplier > 1)
				this._queuedTrans.alphaMultiplier = 1;
			
			// We need to start the alpha halfway through.
			if(getTimer() - this._startTime < halfDur){
				this._activeTrans.alphaMultiplier = 1;
			} else {
				this._activeTrans.alphaMultiplier = 1 - (getTimer() - (this._startTime + halfDur)) / (halfDur);
			}
			
			if(this._activeTrans.alphaMultiplier > 1)
				this._activeTrans.alphaMultiplier = 1;
			
			this._output.draw(active, null, this._activeTrans);
			this._output.draw(queued, null, this._queuedTrans);
			
			return super.render(queued, active);
		}
		
		override public function destroy():void
		{
			super.destroy();
			this._activeBmp.dispose();
			this._activeBmp = null;
			this._queuedBmp.dispose();
			this._queuedBmp = null;
		}
	}
}